class Usuario < ActiveRecord::Base
  has_one :turma
  has_many :materia
  belongs_to :tipousuario
  before_create :hash_senha
  validates_uniqueness_of :matricula
  validates_presence_of :matricula, :senha
  attr_protected :id
  
  def hash_senha
    self.senha = Digest::MD5.hexdigest(self.senha)
  end
  
  def self.authenticate (matricula,pass)
    u=find(:first, :conditions=>["matricula = ?",matricula.to_i])
    return nil if u.nil?
    pass=cod_senha(pass)
    return u if pass==u.senha 
    puts u
    nil
  end  
  
  protected
  def self.cod_senha(pass)
     pass = Digest::MD5.hexdigest(pass)
     return pass
  end
end
